/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.infrastructure.persistence;

import de.digiway.rapidbreeze.server.TestUtil;
import de.digiway.rapidbreeze.server.infrastructure.objectstorage.ObjectStorage;
import de.digiway.rapidbreeze.server.model.download.Download;
import de.digiway.rapidbreeze.server.model.download.DownloadFactory;
import de.digiway.rapidbreeze.server.model.storage.StorageProviderRepository;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;

/**
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
public class DownloadRepositoryStorageIT {

    private DownloadRepositoryStorage repository;
    private StorageProviderRepository storageProviderRepository;
    private Path file;
    private Path storagePath;

    @Before
    public void setup() throws IOException {
        storagePath = TestUtil.getNonExistingTemporaryPath();
        ObjectStorage objectStorage = new ObjectStorage(storagePath);
        repository = new DownloadRepositoryStorage(objectStorage);
        repository.clearAll();
        assertTrue(repository.getDownloads().isEmpty());

        storageProviderRepository = new StorageProviderRepositoryStorage(objectStorage);
        assertTrue(storageProviderRepository.getStorageProviders().isEmpty());
        DownloadFactory.registerStorageProviderRepository(storageProviderRepository);

        file = Files.createTempFile("rapidbreeze", ".tmp");
    }

    @After
    public void tearDown() throws IOException {
        TestUtil.deleteDirectoryRecursive(storagePath);
        Files.deleteIfExists(file);
    }

    @Test
    public void testGetAllDownloads() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST), file);
        assertTrue(repository.getDownloads().isEmpty());

        repository.add(download);
        assertFalse(repository.getDownloads().isEmpty());
        assertEquals(1, repository.getDownloads().size());
        assertEquals(download, repository.getDownloads().get(0));
    }

    @Test
    public void testHasDownload() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download1 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST + "/1"), file);
        repository.add(download1);

        assertFalse(repository.hasDownload("blaaaaaa"));
        assertTrue(repository.hasDownload(download1.getIdentifier()));
    }

    @Test
    public void testGetDownload() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download1 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST + "/1"), file);
        Download download2 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST + "/2"), file);
        repository.add(download1);
        repository.add(download2);

        assertEquals(download1, repository.getDownload(download1.getIdentifier()));
        assertEquals(download2, repository.getDownload(download2.getIdentifier()));
        assertNotSame(download1, download2);
    }

    @Test
    public void testAddDownload() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download1 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST), file);
        Download download2 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST + "/huhu"), file);

        repository.add(download1);
        assertFalse(repository.getDownloads().isEmpty());
        assertEquals(1, repository.getDownloads().size());
        assertEquals(download1, repository.getDownloads().get(0));

        repository.add(download2);
        assertEquals(2, repository.getDownloads().size());
        assertTrue(repository.getDownloads().contains(download1));
        assertTrue(repository.getDownloads().contains(download2));
    }

    @Test
    public void testRemove() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download1 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST), file);
        repository.add(download1);
        assertFalse(repository.getDownloads().isEmpty());

        repository.remove(download1);
        assertTrue(repository.getDownloads().isEmpty());
    }

    @Test
    public void testClearAll() throws Exception {
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        Download download1 = DownloadFactory.createDownload(new URL("http://" + TestProvider.PROVIDER_HOST), file);
        repository.add(download1);
        assertFalse(repository.getDownloads().isEmpty());

        repository.clearAll();
        assertTrue(repository.getDownloads().isEmpty());
    }
}
